package cs342.project1.mathNotebook.xml;

import java.io.*;
import java.util.Collection;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;

/**
 * [insert description of class]
 * @author Gabrielle DuBois
 */
public class DESReader extends NotebookReader {
	private CompressionWriter compressWriter;
	private byte[] desKeyData;

	/**
     * Constructor
     * @param userKey a String to be used to enycpt the data
     */
	public DESReader(InputStream inStream, byte[] userKey){
		super(inStream);
		desKeyData = userKey;
    }//constructor

    /**
     * @see cs342.project1.mathNotebook.xml.NotebookReader#loadStream(java.lang.String)
     */
	public InputStream loadStream(InputStream instream) {
		try {
            // Create a key.
            //byte[] desKeyData = password.getBytes();
            DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey desKey = keyFactory.generateSecret(desKeySpec);

            // Use Data Encryption Standard cipher
            Cipher des = Cipher.getInstance("des");
            des.init(Cipher.DECRYPT_MODE, desKey);

            String decrypted_string = new String("");

            byte[] input = new byte[in.available()];
            in.read(input);
            byte[] output = des.doFinal(input);

			//System.out.println(decrypted_string);
            in = new ByteArrayInputStream(output);
            CompressionReader compressReader = new CompressionReader(in);
            in = compressReader.loadStream( in );
        }
        catch (InvalidKeySpecException e) {System.err.println(e);}
        catch (InvalidKeyException e) {
        	System.err.println(e);
        }
        catch (NoSuchAlgorithmException e) {
            System.err.println(e);
        }
        catch (NoSuchPaddingException e) {System.err.println(e);}
        catch (BadPaddingException e) {System.err.println(e);}
        catch (IllegalBlockSizeException e) {System.err.println(e);}
        catch (IOException e) {System.err.println(e);}

		return in;
	}//loadStream()

}//DESReader
